#!/usr/bin/perl -I ./thirds -w
# Author: Micro Li (microli@sz.webex.com)
# Date: 05/18/2007
#
use strict;
use English;
use Data::Dumper;
use Cwd;


sub main();
sub help_msg();
sub parse_command_line();
sub get_apache_configure($);
sub read_whole_file($);
sub remove_redundant_slash($);
sub uninstall();

my $g_mode              = 1; # 1: install,  0: uninstall
my $G_INST_LOG          = "INST.LOG";
my $G_CCV_CGI_DEF_FILE  = "ccv.script.alias.httpd";

my $g_install_locate    = "/usr/local/";
my $g_ccv_locate 		= "$g_install_locate/ccv";
my $g_apache_conf;
my $g_configure;

exit main();

sub main() {
    if (parse_command_line() != 0) {
        help_msg();
        
        return 1;
    }
    my $old_umask = umask(18); #755
    if ($g_mode == 0) { #uninstall
        if (!-e $G_INST_LOG) {
            print "Error: can not find ccv install log!\nRun \"perl $0 --help\" to get usage message.\n";
            
            return 1;
        }
        my $LOG = read_whole_file($G_INST_LOG);
        
        $LOG =~ m/^httpd conf: ([^\s]+)/;
        $g_apache_conf = $1;
    }
    
    if (!-e $g_apache_conf) {
        print "Can not find apache configure file \"". $g_apache_conf . "\"\n";
        
        return 1;    
    }
        
    my $httpd_conf_content = read_whole_file($g_apache_conf);
    
    my $g_configure = get_apache_configure(\$httpd_conf_content);
    if (!defined($g_configure)) {
        print "Error: can not get apache configure\n";
        
        return 1;
    }
    
    my $cmd = "";    
    if ($g_mode == 0) { #uninstall
        return uninstall();
    }    

	my $logs = "Install ...\n\n"; 
    if (! -e $g_install_locate) {
        $cmd = "mkdir $g_install_locate";                    $logs .= "$cmd\n"; print `$cmd`;   
    }
    $cmd = "tar -xzf ccv-2.0.tar.gz -C $g_install_locate";   $logs .= "$cmd\n"; print `$cmd`;
    
    $cmd = "chmod -R 755 $g_ccv_locate/";                    $logs .= "$cmd\n"; print `$cmd`;
    $cmd = "chmod -R 777 $g_ccv_locate/config";              $logs .= "$cmd\n"; print `$cmd`;
    $cmd = "chmod -R 777 $g_ccv_locate/operate";             $logs .= "$cmd\n"; print `$cmd`;
    $cmd = "chmod -R 777 $g_ccv_locate/web/reports";         $logs .= "$cmd\n"; print `$cmd`;
	
    $cmd = "rm -rf $g_configure->{document_root}/ccv"; 									remove_redundant_slash(\$cmd);$logs .= "$cmd\n"; print `$cmd`;
    $cmd = "rm -rf $g_configure->{document_root}/ccv/operate"; 							remove_redundant_slash(\$cmd);$logs .= "$cmd\n"; print `$cmd`;   
    $cmd = "ln -s $g_ccv_locate/web $g_configure->{document_root}/ccv";  				remove_redundant_slash(\$cmd);$logs .= "$cmd\n"; print `$cmd`;      
    $cmd = "ln -s $g_ccv_locate/operate $g_configure->{document_root}/ccv/operate";  	remove_redundant_slash(\$cmd);$logs .= "$cmd\n"; print `$cmd`;      

    my $cgi_web = "/ccv-cgi/";
    my $cgi_local = $g_ccv_locate . "/";
    		
    my $ccv_http_file = "$g_ccv_locate/config/$G_CCV_CGI_DEF_FILE";
	my $ccv_http_template_file = $ccv_http_file . ".TEMPLATE";
	my $tmp_cgi_web = $cgi_web;
	my $tmp_cgi_local = $cgi_local;
	$tmp_cgi_web =~ s|/|\\/|g;
	$tmp_cgi_local =~ s|/|\\/|g;
	   
    $cmd = "sed -i 's/#CGI_WEB#/$tmp_cgi_web/g' $ccv_http_template_file"; 		$logs .= "$cmd\n"; print `$cmd`;
    $cmd = "sed -i 's/#CGI_LOCAL#/$tmp_cgi_local/g' $ccv_http_template_file"; 	$logs .= "$cmd\n"; print `$cmd`;
    $cmd = "mv $ccv_http_template_file $ccv_http_file";							$logs .= "$cmd\n"; print `$cmd`;

    
	my $inlcude_ccv_cgi_define = "include $g_ccv_locate/config/$G_CCV_CGI_DEF_FILE";
    $cmd = "sed -i.bak-by-ccv -re '/^(include .*" . $G_CCV_CGI_DEF_FILE . ")/d' " . $g_apache_conf;
    $logs .= "$cmd\n"; print `$cmd`;

    $cmd = "sed -i.bak-by-ccv -re '/^DocumentRoot/i \\" . $inlcude_ccv_cgi_define . "' " . $g_apache_conf;
    $logs .= "$cmd\n"; print `$cmd`; 				

    my $H_LOG;
    if (!open($H_LOG, ">", "$g_ccv_locate/$G_INST_LOG")) {
        print "Can not create install log!\n";    
        
        return 1;
    }
    print $H_LOG "httpd conf: $g_apache_conf\n";
    print $H_LOG "ccv: $g_ccv_locate\n";
    print $H_LOG "ccv cgi conf: $inlcude_ccv_cgi_define\n\n";
    print $H_LOG "$logs\n\nInstall finished!\n\n";
    
    print "httpd conf: $g_apache_conf\n";
    print "ccv: $g_ccv_locate\n";
    print "ccv cgi conf: $inlcude_ccv_cgi_define\n\n";
    print "$logs\n\nInstall finished!\n\n";
    
    my $ccv_entry_url = "http://$g_configure->{server_name}/$cgi_web/ccv.pl";
    remove_redundant_slash(\$ccv_entry_url);
    
    my $config_file = "$g_ccv_locate/config/ccv.xml";
    remove_redundant_slash(\$config_file);
    print "1. Pls configure your cvs modules in file $config_file \n";
    print "2. Restart apache then access ccv: $ccv_entry_url\n\n";
    close($H_LOG);

    umask($old_umask);
    
    return 0;    
}

sub remove_redundant_slash($) {
    ${$_[0]} =~ s|/{2,}|/|g;
}

sub parse_command_line(){
    if ($#ARGV < 0 || $ARGV[0] eq "--help") {
        return 1;
    }
        
    my $cmd_line = join (" ", @ARGV);
    if ($cmd_line =~ m/\-U/) {
        $g_mode = 0;
        
        return 0;
    }      
    
    if ($cmd_line =~ m/\-\-apache-conf=([^\s]+)/) {
        $g_apache_conf = $1;
    }  else {
        return 2;   
    }

    if ($cmd_line =~ m/\-\-prefix=([^\s]+)/) {
        $g_install_locate = $1;
        $g_install_locate =~ s|\\|/|g;
        remove_redundant_slash(\$g_install_locate);
        if ($g_install_locate =~ m/(.*)\/$/) {
        	$g_install_locate = $1;
        }
        
        $g_ccv_locate = "$g_install_locate/ccv";
    }                

    return 0;
}

sub get_apache_configure($) {
    my $httpd_conf_content = ${$_[0]};
    my $cgi_web;
    my $cgi_local;
    if ($httpd_conf_content =~ /\nDocumentRoot\s+("?)([^\s]+)\1/s) {
        $g_configure->{document_root} = $2;
    }
    
    if ($httpd_conf_content =~ /\nServerName\s+("?)([^\s]+)\1/s) {
        $g_configure->{server_name} = $2;
    }
	    
    if (!defined($g_configure->{server_name})) {
    	if ($httpd_conf_content =~ /\nListen\s+("?)([^\s]+)\1/s) { #"
    		$g_configure->{server_name} = $2;
    	}
    }      
    
    if ($httpd_conf_content =~ /\nScriptAlias\s+("?)([^\s]+)\1\s+("?)([^\s]+)\3\s*\n/s) {
        $g_configure->{cgi_web} = $2;
        $g_configure->{cgi_local} = $4;    
    }
    
    if (!defined($g_configure->{document_root}) || !defined($g_configure->{server_name})) {
        print "Your apache configure is not OK, pls check it first!\n";
        
        return undef;
    }
    
    return $g_configure;
}

sub help_msg(){        
    print <<EOF;

Usage:   perl $0 [-U] --apache-conf=/apache/configure/file [--prefix=/ccv/installed/directory]
If --prefix option is omited, it will be installed to /usr/local/ccv.

Example: 
  * Install
    perl $0 --apache-conf=/usr/local/apache2/conf/httpd.conf --prefix=/ccv/install/location
  
  * Uninstall
    perl $0 -U #Run this in ccv installed directory
     
     
    
EOF
}

sub read_whole_file($) {
    my $file = $_[0];
    if (!open(INPUT, "<", $file)) {
        return undef;   
    }
    
    my $old = $/;
    undef $/;
    my $content = <INPUT>;
    close INPUT;
    $/ = $old;
    
    return $content;
}

sub uninstall() {
    my $cmd = "";
    print "Unistall ccv ...\n\n";
    if (-e "$g_configure->{document_root}/ccv") {
        $cmd = "rm -rf $g_configure->{document_root}/ccv";
        remove_redundant_slash(\$cmd);
        print "$cmd\n"; print `$cmd`;
    }

    $cmd = "sed -i.bak-by-ccv-uni -re '/^(include .*" . $G_CCV_CGI_DEF_FILE . ")/d' " . $g_apache_conf;
    print "$cmd\n"; print `$cmd`; 

    print "\n\nUnistall finished!!\n\n";
    
    return 0;    
}
